Package org.python.pydev.editor

Source Code of org.python.pydev.editor.PyEditConfigurationWithoutEditor

/**
* Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Eclipse Public License (EPL).
* Please see the license.txt included with this distribution for details.
* Any modifications to this file must keep this entire header intact.
*/
package org.python.pydev.editor;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
import org.eclipse.jface.text.reconciler.MonoReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
import org.eclipse.ui.texteditor.spelling.SpellingService;
import org.python.pydev.core.IPythonPartitions;
import org.python.pydev.editor.autoedit.DefaultIndentPrefs;
import org.python.pydev.editor.autoedit.PyAutoIndentStrategy;
import org.python.pydev.editor.codecompletion.PyContentAssistant;
import org.python.pydev.editor.preferences.PydevEditorPrefs;
import org.python.pydev.plugin.PydevPlugin;
import org.python.pydev.ui.ColorAndStyleCache;

import com.aptana.shared_core.string.FastStringBuffer;

public class PyEditConfigurationWithoutEditor extends TextSourceViewerConfiguration {

    private ColorAndStyleCache colorCache;

    private PyAutoIndentStrategy autoIndentStrategy;

    private String[] indentPrefixes = { "    ", "\t", "" };

    private PresentationReconciler reconciler;

    private PyCodeScanner codeScanner;

    private PyColoredScanner commentScanner, stringScanner, backquotesScanner;

    public PyContentAssistant pyContentAssistant = new PyContentAssistant();

    private final Object lock = new Object();

    public PyEditConfigurationWithoutEditor(ColorAndStyleCache colorManager, IPreferenceStore preferenceStore) {
        super(preferenceStore);
        colorCache = colorManager;
    }

    /**
     * Has to return all the types generated by partition scanner.
     *
     * The SourceViewer will ignore double-clicks and any other configuration behaviors inside any partition not declared here
     */
    public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
        return new String[] { IDocument.DEFAULT_CONTENT_TYPE, IPythonPartitions.PY_COMMENT,
                IPythonPartitions.PY_BACKQUOTES, IPythonPartitions.PY_SINGLELINE_STRING1,
                IPythonPartitions.PY_SINGLELINE_STRING2, IPythonPartitions.PY_MULTILINE_STRING1,
                IPythonPartitions.PY_MULTILINE_STRING2 };
    }

    @Override
    public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
        return IPythonPartitions.PYTHON_PARTITION_TYPE;
    }

    /**
     * Cache the result, because we'll get asked for it multiple times Now, we always return the PyAutoIndentStrategy. (even on commented lines).
     *
     * @return PyAutoIndentStrategy which deals with spaces/tabs
     */
    public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
        return new IAutoEditStrategy[] { getPyAutoIndentStrategy() };
    }

    @Override
    public IReconciler getReconciler(ISourceViewer sourceViewer) {
        if (fPreferenceStore == null || !fPreferenceStore.getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED))
            return null;

        SpellingService spellingService = EditorsUI.getSpellingService();
        if (spellingService.getActiveSpellingEngineDescriptor(fPreferenceStore) == null)
            return null;

        //Overridden (just) to return a PyReconciler!
        IReconcilingStrategy strategy = new PyReconciler(sourceViewer, spellingService);

        MonoReconciler reconciler = new MonoReconciler(strategy, false);
        reconciler.setIsIncrementalReconciler(false);
        reconciler.setProgressMonitor(new NullProgressMonitor());
        reconciler.setDelay(500);
        return reconciler;
    }

    /**
     * Cache the result, because we'll get asked for it multiple times Now, we always return the PyAutoIndentStrategy. (even on commented lines).
     *
     * @return PyAutoIndentStrategy which deals with spaces/tabs
     */
    public PyAutoIndentStrategy getPyAutoIndentStrategy() {
        if (autoIndentStrategy == null) {
            autoIndentStrategy = new PyAutoIndentStrategy();
        }
        return autoIndentStrategy;
    }

    /**
     * Recalculates indent prefixes based upon preferences
     *
     * we hold onto the same array SourceViewer has, and write directly into it. This is because there is no way to tell SourceViewer that indent prefixes have changed. And we need this functionality
     * when user resets the tabs vs. spaces preference
     */
    public void resetIndentPrefixes() {
        IPreferenceStore prefs = PydevPlugin.getDefault().getPreferenceStore();
        int tabWidth = DefaultIndentPrefs.getStaticTabWidth();
        FastStringBuffer spaces = new FastStringBuffer(8);

        for (int i = 0; i < tabWidth; i++) {
            spaces.append(" ");
        }

        boolean spacesFirst = prefs.getBoolean(PydevEditorPrefs.SUBSTITUTE_TABS)
                && !(getPyAutoIndentStrategy()).getIndentPrefs().getForceTabs();

        if (spacesFirst) {
            indentPrefixes[0] = spaces.toString();
            indentPrefixes[1] = "\t";
        } else {
            indentPrefixes[0] = "\t";
            indentPrefixes[1] = spaces.toString();
        }
    }

    /**
     * Prefixes used in shift-left/shift-right editor operations
     *
     * shift-right uses prefix[0] shift-left removes a single instance of the first prefix from the array that matches
     *
     * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getIndentPrefixes(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
     */
    public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
        resetIndentPrefixes();
        sourceViewer.setIndentPrefixes(indentPrefixes, contentType);
        return indentPrefixes;
    }

    /**
     * Just the default double-click strategy for now. But we should be smarter.
     *
     * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getDoubleClickStrategy(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
     */
    public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
        return new PyDoubleClickStrategy(contentType);
    }

    /**
     * TabWidth is defined inside pydev preferences.
     *
     * Python uses its own tab width, since I think that its standard is 8
     */
    public int getTabWidth(ISourceViewer sourceViewer) {
        return DefaultIndentPrefs.getStaticTabWidth();
    }

    public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {

        synchronized (lock) {
            if (reconciler == null) {
                reconciler = new PresentationReconciler();
                reconciler.setDocumentPartitioning(IPythonPartitions.PYTHON_PARTITION_TYPE);

                DefaultDamagerRepairer dr;

                // DefaultDamagerRepairer implements both IPresentationDamager, IPresentationRepairer
                // IPresentationDamager::getDamageRegion does not scan, just
                // returns the intersection of document event, and partition region
                // IPresentationRepairer::createPresentation scans
                // gets each token, and sets text attributes according to token

                // We need to cover all the content types from PyPartitionScanner

                // Comments have uniform color
                commentScanner = new PyColoredScanner(colorCache, PydevEditorPrefs.COMMENT_COLOR);
                dr = new DefaultDamagerRepairer(commentScanner);
                reconciler.setDamager(dr, IPythonPartitions.PY_COMMENT);
                reconciler.setRepairer(dr, IPythonPartitions.PY_COMMENT);

                // Backquotes have uniform color
                backquotesScanner = new PyColoredScanner(colorCache, PydevEditorPrefs.BACKQUOTES_COLOR);
                dr = new DefaultDamagerRepairer(backquotesScanner);
                reconciler.setDamager(dr, IPythonPartitions.PY_BACKQUOTES);
                reconciler.setRepairer(dr, IPythonPartitions.PY_BACKQUOTES);

                // Strings have uniform color
                stringScanner = new PyColoredScanner(colorCache, PydevEditorPrefs.STRING_COLOR);
                dr = new DefaultDamagerRepairer(stringScanner);
                reconciler.setDamager(dr, IPythonPartitions.PY_SINGLELINE_STRING1);
                reconciler.setRepairer(dr, IPythonPartitions.PY_SINGLELINE_STRING1);
                reconciler.setDamager(dr, IPythonPartitions.PY_SINGLELINE_STRING2);
                reconciler.setRepairer(dr, IPythonPartitions.PY_SINGLELINE_STRING2);
                reconciler.setDamager(dr, IPythonPartitions.PY_MULTILINE_STRING1);
                reconciler.setRepairer(dr, IPythonPartitions.PY_MULTILINE_STRING1);
                reconciler.setDamager(dr, IPythonPartitions.PY_MULTILINE_STRING2);
                reconciler.setRepairer(dr, IPythonPartitions.PY_MULTILINE_STRING2);

                // Default content is code, we need syntax highlighting
                ICodeScannerKeywords codeScannerKeywords = null;
                if (sourceViewer instanceof IAdaptable) {
                    IAdaptable iAdaptable = (IAdaptable) sourceViewer;
                    codeScannerKeywords = (ICodeScannerKeywords) iAdaptable.getAdapter(ICodeScannerKeywords.class);
                    codeScanner = new PyCodeScanner(colorCache, codeScannerKeywords);
                } else {
                    codeScanner = new PyCodeScanner(colorCache);
                }
                dr = new DefaultDamagerRepairer(codeScanner);
                reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
                reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
            }
        }

        return reconciler;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getInformationControlCreator(org.eclipse.jface.text.source.ISourceViewer)
     */
    public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
        return PyContentAssistant.createInformationControlCreator(sourceViewer);
    }

    /**
     * Returns the settings for the given section.
     *
     * @param sectionName the section name
     * @return the settings
     * @since pydev 1.3.5
     */
    protected IDialogSettings getSettings(String sectionName) {
        IDialogSettings settings = PydevPlugin.getDefault().getDialogSettings().getSection(sectionName);
        if (settings == null)
            settings = PydevPlugin.getDefault().getDialogSettings().addNewSection(sectionName);

        return settings;
    }

    //updates the syntax highlighting for the specified preference
    //assumes that that editor colorCache has been updated with the
    //new named color
    public void updateSyntaxColorAndStyle() {
        synchronized (lock) {

            if (reconciler != null) {
                //always update all (too much work in keeping this synchronized by type)
                if (codeScanner != null) {
                    codeScanner.updateColors();
                }

                if (commentScanner != null) {
                    commentScanner.updateColorAndStyle();
                }

                if (stringScanner != null) {
                    stringScanner.updateColorAndStyle();
                }

                if (backquotesScanner != null) {
                    backquotesScanner.updateColorAndStyle();
                }
            }
        }
    }

}
TOP

Related Classes of org.python.pydev.editor.PyEditConfigurationWithoutEditor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.